package com.andy.beef.main.fragment

import android.content.Intent
import android.os.Bundle
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.andy.beef.R
import com.andy.beef.app.util.LocalDataUtil
import com.andy.beef.databinding.MainFragmentMineBinding
import com.andy.beef.login.activity.LoginActivity
import com.andy.beef.main.viewmodel.MineViewModel
import com.andy.module.base.BaseVbFragment
import com.andy.ui.ext.clickNoRepeat

/**
 * @creator:LuoFan
 * @date:2022/9/30
 * @description:
 */
class MineFragment : BaseVbFragment<MineViewModel, MainFragmentMineBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        LocalDataUtil.getUserInfo().apply {
            mViewBind.tvMineName.text = this.userName
            mViewBind.tvMineMobile.text = this.phone
            mViewBind.tvRole.text = this.roleStr
        }
        mViewBind.tvLogout.clickNoRepeat {
            showExitDialog()
        }
    }

    private fun showExitDialog() {
        MaterialDialog(requireContext()).show {
            title(R.string.dialog_title_tip)
            positiveButton(R.string.dialog_confirm, click = {
                toExit()
            })
            negativeButton(R.string.dialog_cancel)
            lifecycleOwner(viewLifecycleOwner)
        }
    }

    private fun toExit() {
        LocalDataUtil.clearToken()
        requireActivity().finish()
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        intent.setClass(requireActivity(), LoginActivity::class.java)
        requireActivity().startActivity(intent)
    }

}